/**
 * Title: RoleController.java
 *
 * @author wujin
 * @date 2019年12月30日
 * @version 1.0
 */
package com.pm.controller.task;

import com.github.pagehelper.PageInfo;
import com.pm.common.Pages;
import com.pm.common.Response;
import com.pm.common.annotation.CurrentUser;
import com.pm.common.annotation.MyLog;
import com.pm.common.annotation.UserLoginToken;
import com.pm.common.exception.CommonException;
import com.pm.common.exception.ExpCodeState;
import com.pm.entity.po.log.TaskSubtasksCheckLog;
import com.pm.entity.po.security.User;
import com.pm.entity.po.task.*;
import com.pm.entity.vo.RepeatSubtasksVo;
import com.pm.entity.vo.StateVo;
import com.pm.entity.vo.task.TaskTargetVo;
import com.pm.service.log.TaskSubtasksCheckLogService;
import com.pm.service.task.TaskService;
import com.pm.service.task.TaskSubtasksService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Title: RoleController
 *
 * @author wujin
 * @date 2019年12月30日
 */
@Api(tags = "目标任务管理")
@ApiModel
@UserLoginToken
@RestController
@RequestMapping("/distribution")
public class TaskSubtasksController {

    @Autowired
    private TaskSubtasksService distributionService;
    @Autowired
    TaskSubtasksCheckLogService taskSubtasksCheckLogService;
    @Autowired
    TaskService taskService;

    @ApiOperation("新增接口")
    @PostMapping("/add")
    @ResponseBody
    @MyLog(value = "新增指标信息")
    @Transactional(rollbackFor = CommonException.class)
    public Response<Integer> add(@RequestBody TaskSubtasks taskDistribution) {
        Response<Integer> response = null;
        Task task = new Task();
        task.setId(taskDistribution.getTaskId());
        task = taskService.load(task);
        /*if (task != null) {
            taskDistribution.setStatus();
        }*/
        Integer result = distributionService.add(taskDistribution);
        if (taskDistribution.getDismantle() == 1) {
            for (TaskSubtasks sub : taskDistribution.getSubtasks()) {
                sub.setParentId(taskDistribution.getId());
                sub.setTaskId(taskDistribution.getTaskId());
                result += distributionService.add(sub);
            }
        }
        if (result > 0) {
            response = new Response<>(ExpCodeState.add_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.add_data_failed);
        }
        return response;
    }

    @ApiOperation("更新接口")
    @PostMapping("/update")
    @ResponseBody
    @MyLog(value = "编辑指标信息")
    @Transactional(rollbackFor = CommonException.class)
    public Response<Integer> update(@RequestBody TaskSubtasks taskDistribution) {
        Response<Integer> response = null;
        Integer result = distributionService.update(taskDistribution);
        // 删除目标任务的子任务
        Integer resultNum = distributionService.removeTaskByParentId(taskDistribution);
        if (null != taskDistribution.getDismantle() && taskDistribution.getDismantle() == 1) {
            for (TaskSubtasks sub : taskDistribution.getSubtasks()) {
                sub.setParentId(taskDistribution.getId());
                result += distributionService.add(sub);
            }
        }
        if (result > 0) {
            response = new Response<>(ExpCodeState.update_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.update_data_failed);
        }
        return response;
    }

    @ApiOperation("提交审核")
    @PostMapping("/auditing")
    @ResponseBody
    @MyLog(value = "编辑指标信息")
    @Transactional(rollbackFor = CommonException.class)
    public Response<Integer> auditing(TaskSubtasks taskDistribution) {
        Response<Integer> response = null;
        TaskSubtasksCheckLog tlog = new TaskSubtasksCheckLog();
        tlog.setStatus(0);
        tlog.setRead(0);
        tlog.setQuarter(taskDistribution.getAllNumber());
        taskDistribution.setAllNumber(null);
        Integer result = distributionService.update(taskDistribution);
        taskSubtasksCheckLogService.add(tlog);
        if (result > 0) {
            response = new Response<>(ExpCodeState.update_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.update_data_failed);
        }
        return response;
    }

    @ApiOperation("提交审核_新")
    @PostMapping("/auditingNwe")
    @ResponseBody
    @MyLog(value = "编辑指标信息")
    @Transactional(rollbackFor = CommonException.class)
    public Response<Integer> auditingNwe(TaskSubtasks taskDistribution, @CurrentUser User user) {
        Response<Integer> response = null;
        TaskSubtasksCheckLog tlog = new TaskSubtasksCheckLog();
        tlog.setQuarter(taskDistribution.getQuarter());
        tlog.setStatus(0);
        tlog.setRead(0);
        // 申请人
        tlog.setApplicat(user.getUserId());
        taskDistribution.setAllNumber(null);
        tlog.setTaskId(taskDistribution.getTaskId());
        Integer result = distributionService.updateNew(taskDistribution);
        taskSubtasksCheckLogService.add(tlog);
        if (result > 0) {
            response = new Response<>(ExpCodeState.update_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.update_data_failed);
        }
        return response;
    }

    @ApiOperation("删除接口")
    @PostMapping("/remove")
    @ResponseBody
    @MyLog(value = "删除指标信息")
    public Response<Integer> remove(@RequestBody TaskSubtasks taskDistribution) {
        Response<Integer> response = null;
        Integer result = distributionService.remove(taskDistribution);
        if (result > 0) {
            response = new Response<>(ExpCodeState.remove_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.remove_data_failed);
        }
        return response;
    }

    @ApiOperation("单个对象查询接口")
    @PostMapping("/load")
    @ResponseBody
    public Response<TaskSubtasks> load(@RequestBody TaskSubtasks taskDistribution) {
        Response<TaskSubtasks> response = null;
        TaskSubtasks result = distributionService.load(taskDistribution);
        response = new Response<TaskSubtasks>(ExpCodeState.query_data_success, result);
        return response;
    }

    @ApiOperation("查询状态")
    @PostMapping("/getstate")
    @ResponseBody
    @MyLog(value = "查询状态")
    public Response<List<StateVo>> getstate(@RequestBody TaskSubtasks taskDistribution) {
        Response<List<StateVo>> response = null;
        List<StateVo> result = distributionService.getstate(taskDistribution);
        response = new Response<List<StateVo>>(ExpCodeState.query_data_success, result);
        return response;
    }


    @ApiOperation("修改通过状态")
    @PostMapping("/updateAdopt")
    @ResponseBody
    @MyLog(value = "修改通过状态")
    public Response<Integer> updateAdopt(@RequestBody TaskSubtasks taskDistribution, @CurrentUser
            User user) {
        // 修改主表状态
        Response<Integer> response = null;
        Integer result = distributionService.updateAdopt(taskDistribution);
        // 修改信息状态
        TaskSubtasksCheckLog tlog = new TaskSubtasksCheckLog();
        if (taskDistribution.getStatus() == -1) {
            // 审核人
            tlog.setAuditor(user.getUserId());
            tlog.setTaskId(taskDistribution.getTaskId());
            tlog.setAddTime(new Date());
            tlog.setMessage(taskDistribution.getMessage());
            tlog.setQuarter(taskDistribution.getQuarter());
            tlog.setRead(0);
            tlog.setApplicat(null);
            tlog.setStatus(taskDistribution.getStatus());
            taskSubtasksCheckLogService.addAuditor(tlog);
        }
        // 审核人
        tlog.setAuditor(user.getUserId());
        tlog.setTaskId(taskDistribution.getTaskId());
        tlog.setUpdateTime(new Date());
        tlog.setStatus(taskDistribution.getStatus());
        taskSubtasksCheckLogService.updateAdopt(tlog);
        if (result > 0) {
            response = new Response<>(ExpCodeState.update_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.update_data_failed);
        }
        return response;
    }

    @ApiOperation("全部列表查询接口带部门")
    @PostMapping("/getListALL")
    @ResponseBody
    public Response<List<TaskSubtasks>> getListALL(@RequestBody TaskSubtasks taskDistribution) {
        List<TaskSubtasks> result = distributionService.getListALL(taskDistribution);
        Response<List<TaskSubtasks>> res = new Response<List<TaskSubtasks>>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("全部列表查询接口")
    @PostMapping("/getList")
    @ResponseBody
    public Response<List<TaskSubtasks>> getList(@RequestBody TaskSubtasks taskDistribution) {
        List<TaskSubtasks> result = distributionService.getList(taskDistribution);
        Response<List<TaskSubtasks>> res = new Response<>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("查询任务进度列表")
    @PostMapping("/getTaskSpeedList")
    @ResponseBody
    public Response<List<TaskSubtasks>> getTaskSpeedList(@RequestBody TaskSubtasks taskDistribution) {
        List<TaskSubtasks> result = distributionService.getTaskSpeedList(taskDistribution);
        Response<List<TaskSubtasks>> res = new Response<>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("分页查询接口")
    @PostMapping("/getListByPage/{pageNum}/{pageSize}")
    @ResponseBody
    @MyLog(value = "查询指标信息")
    public Response<PageInfo<TaskSubtasks>> getList(@RequestBody TaskSubtasks taskDistribution, Pages page) {
        PageInfo<TaskSubtasks> result = distributionService.getListByPage(taskDistribution, page);
        Response<PageInfo<TaskSubtasks>> res = new Response<PageInfo<TaskSubtasks>>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("季度完成率列表查询接口带部门")
    @PostMapping("/getListQuarter")
    @ResponseBody
    public Response<List<TaskSubtasks>> getListQuarter(@RequestBody TaskSubtasks taskDistribution) {
        List<TaskSubtasks> result = distributionService.getListQuarter(taskDistribution);
        Response<List<TaskSubtasks>> res = new Response<List<TaskSubtasks>>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("计划汇总列表查询")
    @PostMapping("/getListTaskSubtasks")
    @ResponseBody
    public Response<List<TaskSubtasks>> getListTaskSubtasks(@RequestBody TaskSubtasks taskSubtasks) {
        List<TaskSubtasks> result = distributionService.getListTaskSubtasks(taskSubtasks);
        Response<List<TaskSubtasks>> res = new Response<List<TaskSubtasks>>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("目标任务模糊查询")
    @PostMapping("/repeatSubtasks")
    @ResponseBody
    public Response<List<RepeatSubtasksVo>> repeatSubtasks(String targetName) {
        List<RepeatSubtasksVo> result = distributionService.repeatSubtasks(targetName);
        Response<List<RepeatSubtasksVo>> res = new Response<List<RepeatSubtasksVo>>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("目标任务查重")
    @PostMapping("/repeat")
    @ResponseBody
    public Response<List<RepeatSubtasksVo>> repeat() {
        List<RepeatSubtasksVo> result = distributionService.repeat();
        Response<List<RepeatSubtasksVo>> res = new Response<List<RepeatSubtasksVo>>(ExpCodeState.query_data_success, result);
        return res;
    }


    @ApiOperation("修改灯的状态")
    @PostMapping("/updateLamp")
    @ResponseBody
    @MyLog(value = "编辑重点任务打分")
    public Response<Integer> updateLamp(@RequestBody TaskSubtasks taskSubtasks) {
        Response<Integer> response = null;
        Integer result = distributionService.updateLamp(taskSubtasks);
        if (result > 0) {
            response = new Response<>(ExpCodeState.update_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.update_data_failed);
        }
        return response;
    }

    @ApiOperation("修改亮灯情况为全绿")
    @PostMapping("/updateSocre")
    @ResponseBody
    @MyLog(value = "修改亮灯情况为全绿")
    public Response<Integer> updateSocre(@RequestBody TaskSubtasks taskSubtasks) {
        Response<Integer> response = null;
        Integer result = distributionService.updateSocre(taskSubtasks);
        if (result > 0) {
            response = new Response<>(ExpCodeState.update_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.update_data_failed);
        }
        return response;
    }

    @ApiOperation("亮灯比例")
    @PostMapping("/LampProportion")
    @ResponseBody
    public Response<List<Map<String, Object>>> getLampProportion() {
        List<Map<String, Object>> result = distributionService.getLampProportion();
        Response<List<Map<String, Object>>> res = new Response<List<Map<String, Object>>>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("目标任务亮灯比例")
    @PostMapping("/LampProportionNew")
    @ResponseBody
    public Response<List<Map<String, Object>>> getLampProportionNew(@RequestBody LampProportion lampProportion) {
        List<Map<String, Object>> result = distributionService.getLampProportionNew(lampProportion.getTaskId());
        Response<List<Map<String, Object>>> res = new Response<List<Map<String, Object>>>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("获取工作目标完成进度")
    @PostMapping("/getTaskTargetFinishRate")
    @ResponseBody
    public Response<List<TaskTargetPo>> getTaskTargetFinishRate(@RequestBody TaskTargetVo taskTargetVo) {
        List<TaskTargetPo> result = distributionService.getTaskTargetFinishRate(taskTargetVo);
        Response<List<TaskTargetPo>> res = new Response<>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("获取任务目标总量及亮等比例")
    @PostMapping("/getTaskTargetLightUpRate")
    @ResponseBody
    public Response<List<TaskTargetLightUpVo>> getTaskTargetLightUpRate(@RequestBody TaskTargetVo taskTargetVo) {
        List<TaskTargetLightUpVo> result = distributionService.getTaskTargetLightUpRate(taskTargetVo);
        Response<List<TaskTargetLightUpVo>> res = new Response<>(ExpCodeState.query_data_success, result);
        return res;
    }

}
